﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Net.Sockets;
using System.Text;

namespace Ntalk
{
    public partial class MainPage : PhoneApplicationPage, IPageReceiver
    {
        private string hashMd5random = null;
        private string hostClient = null;
        private string portClient = null;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ((App)Application.Current).CreateConnection("ns-server.epita.fr", 4242);
        }

        private String md5response()
        {
            return MD5Core.GetHashString("<hash md5 aléatoire>-<host client>/<port client><pass socks>");
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ((App)Application.Current).connected = false;
        }

        public void receiveMessage(String dest, String message)
        {

        }

        public void receiveData(String data)
        {
            String[] arrayValue = data.Split(' ');
            if (arrayValue[0] == "salut")
            {
                MessageBox.Show("MD5: " + arrayValue[2] + "\nHost: " + arrayValue[3] + "\nPort: " + arrayValue[4]);
                this.hashMd5random = arrayValue[2];
                this.hostClient = arrayValue[3];
                this.portClient = arrayValue[4];
                ((App)Application.Current).sendMessage("auth_ag ext_user none none");
            }
            else if (data.CompareTo("rep 002 -- cmd end") == 0)
            {
                String packet = "ext_user_log " + identifiant.Text;
                String md5 = hashMd5random + "-" + hostClient + "/";
                md5 = md5 + portClient + mdp.Password;
                MessageBox.Show(md5);
                packet = packet + " " + MD5Core.GetHashString(md5).ToLower() +" " + "NTalk" + " SurmonWindowsPhone";
                MessageBox.Show(packet);
                ((App)Application.Current).sendMessage(packet);
            }
            else if (data.IndexOf("rep ") == 0 && data.CompareTo("rep 033 -- ext user identification fail") != 0)
            {
                MessageBox.Show(data);
                NavigationService.Navigate(new Uri("/Contacts.xaml", UriKind.Relative));
            }
            else
            {
                MessageBox.Show("Mauvais mot de passe/identifiant");
            }
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void mdp_PasswordChanged(object sender, RoutedEventArgs e)
        {

        }
    }
}